package com.voltmao.webpro.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.voltmao.webpro.utils.obd.po.OBDUserInfo;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class JsonUtil {
    private static Gson gson = new Gson();
    //JsonPrimitive可以获取转义的字符串
    //JavaBean转换Json字符串
    public static String ObjectToJSON(Object object) {
        Gson g = new GsonBuilder()
                .disableHtmlEscaping()
                .setDateFormat("yyyy-MM-dd HH:mm")
                .registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory())
                .create();

        return g.toJson(object);
    }

    public static String ObjectToJSON(Object map, Class clazz) {

        Gson g = new GsonBuilder()
                .setPrettyPrinting()// 调教格式
                .disableHtmlEscaping()//默认是GSON把HTML 转义的
                .registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory())
                .create();


        return g.toJson(map);
    }

    public static Object ParsJSON(String json, Class clazz) {
        //Json字符串转JavaBean
        return gson.fromJson(json.replaceAll("=,", "='',"), clazz);
    }

    public static LinkedTreeMap<String, Object> ToMap(String json) {
        GsonBuilder gb = new GsonBuilder();
        gb.registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory());
        Gson g = gb.create();

        return g.fromJson(json, new TypeToken<Map<String, Object>>() {
        }.getType());
    }

    public static List<Map> ToList(String json) {
        GsonBuilder gb = new GsonBuilder();
        gb.registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory());
        Gson g = gb.create();

        return g.fromJson(json, new TypeToken<List<Map>>() {
        }.getType());
    }

    private JsonUtil() {

    }

    public static void main(String[] args) {
        String json = "{\"body\":{\"agentId\":0,\"carInfo\":[{\"carId\":\"332470\",\"chePai\":\"\",\"deviceNo\":\"\",\"isBandObd\":\"0\"}],\"channelId\":\"e3902eb43b9d25c97793f5a2182a74d2\",\"custId\":326952,\"head_path\":\"/basedata/hc_bqww.png\",\"isLoginFlag\":1,\"typeId\":3,\"userId\":314363,\"userName\":\"6e47a47355e847bf80a3ca5fcf7e7341\"},\"header\":{\"alert\":true,\"code\":0,\"message\":\"loadData\"}}";

        OBDUserInfo map = (OBDUserInfo) ParsJSON(json, OBDUserInfo.class);
    }
}

class NullStringToEmptyAdapterFactory<T> implements TypeAdapterFactory {
    @SuppressWarnings("unchecked")
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
        Class<T> rawType = (Class<T>) type.getRawType();
        if (rawType != String.class) {
            return null;
        }
        return (TypeAdapter<T>) new StringNullAdapter();
    }
}

class StringNullAdapter extends TypeAdapter<String> {
    @Override
    public String read(JsonReader reader) throws IOException {
        // TODO Auto-generated method stub
        if (reader.peek() == JsonToken.NULL) {
            reader.nextNull();
            return "";
        }
        return reader.nextString();
    }

    @Override
    public void write(JsonWriter writer, String value) throws IOException {
        // TODO Auto-generated method stub
        if (value == null) {
            writer.nullValue();
            return;
        }
        writer.value(value);
    }
}